# -*- coding: utf-8 -*-
from django.db import models
from django.utils import simplejson
import settings


class Server(models.Model):
    order = models.PositiveIntegerField(default=0)
    enabled = models.BooleanField(default=True)
    host = models.CharField(max_length=255,
        help_text='Example: ya.ru')
    description = models.CharField(blank=True, max_length=255)
    agent_port = models.PositiveIntegerField(blank=True,
        default=settings.DEFAULT_AGENT_PORT)

    def __unicode__(self):
        if self.description:
            return u'%s (%s)' (self.host, self.description)
        else:
            return self.host

    class Meta:
        ordering = ('host',)


class ServerGroup(models.Model):
    order = models.PositiveIntegerField(default=0)
    description = models.CharField(max_length=255)
    class Meta:
        ordering = ('description',)


class ServerGroupItem(models.Model):
    server_group = models.ForeignKey(ServerGroup)
    order = models.IntegerField()
    server = models.ForeignKey(Server)
    class Meta:
        ordering = ('order',)


class CheckError(models.Model):
    server = models.ForeignKey(Server)
    datetime = models.DateTimeField(auto_now_add=True)
    error = models.TextField()

    def __unicode__(self):
        return u'Error %s' % self.datetime


class Check(models.Model):
    server = models.ForeignKey(Server)
    datetime = models.DateTimeField(auto_now_add=True)
    data = models.TextField(blank=True)

    def __unicode__(self):
        return u'Check %s' % self.datetime

    def get_data(self):
        return simplejson.loads(self.data)


class Task(models.Model):
    server = models.ForeignKey(Server)
